package Controllers;

import Entity.Project;
import Service.IProjectService;
import Util.CharacterUtil;
import Util.Constant;
import Util.DateUtil;
import Util.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProjectController {

    @Autowired
    private IProjectService iProjectService;

    @RequestMapping(value = "/api/GetAllProjectByOffset", produces = "text/plain;charset=utf-8")
    public String GetAllProject(String start, String offset) {
        if (start == null || offset == null) {
            return Constant.ERROR_10001 + "";
        }
        int start_ = Integer.parseInt(start), offset_ = Integer.parseInt(offset);
        if (start_ < 0 || offset_ <= 0) {
            return Constant.ERROR_10003 + "";
        }
        List<Project> projects = iProjectService.selectByPrimaryKeyOffset(start_, offset_);
        for (Project p : projects) {
            p.setDemonstration(Constant.IMAGE_HEAD_ADDRESS + p.getDemonstration());
        }
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(projects);
        System.out.println("Project:" + json.toString());
        return json;
    }

    @RequestMapping(value = "/api/GetAllProjectTitle", produces = "text/plain;charset=utf-8")
    public String GetAllProjectTitle() {
        List<Project> projectTitles = iProjectService.getAllProjectTitle();
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(projectTitles);
        System.out.println("projectTitles:" + json.toString());
        return json;
    }

    @RequestMapping(value = "/api/GetProjectByType", produces = "text/plain;charset=utf-8")
    public String GetProjectByType(String type) {
        List<String> projectTitles = iProjectService.getProjectByType(type);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(projectTitles);
        System.out.println("projectTitles:" + json.toString());
        return json;
    }

    @RequestMapping(value = "/api/GetAllProjectCount", produces = "text/plain;charset=utf-8")
    public String GetAllProjectCount() {
        int count = iProjectService.getAllProjectCount();
        System.out.println("projectCounts:" + count);
        return count + "";
    }

    @RequestMapping(value = "/api/SearchProject", produces = "text/plain;charset=utf-8")
    public String SearchProject(String index) {
        System.out.println("index:" + index);
        List<Project> projectTitles = iProjectService.searchProject(index);
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(projectTitles);
        System.out.println("SearchTitles:" + json.toString());
        return json;
    }

    @RequestMapping(value = "/api/GetProjectById", produces = "text/plain;charset=utf-8")
    public String GetProjectById(String id) {
        if (id == null) {
            return Constant.ERROR_10001 + "";
        }
        Long mID = Long.parseLong(id.trim());
        Project project = iProjectService.selectByPrimaryKey(mID);
        project.setDemonstration( CharacterUtil.divideStringByCondition(project.getDemonstration(),"，"));
        Gson gson = new GsonBuilder().create();
        String json = gson.toJson(project);
        System.out.println("Project:" + json.toString());
        return json;
    }

    @RequestMapping(value = "/api/InsertProject", produces = "text/plain;charset=utf-8")
    public String InsertProject(@RequestBody String info, MultipartFile file, HttpServletRequest request) {
        System.out.println("InsertProject:" + info);
        JSONObject jo = new JSONObject();
        Map<String, Object> m = (Map<String, Object>) jo.parse(info);
        if (m != null) {
            return Constant.ERROR_10001 + "";
        }
        Project project = new Project();
        project.setTitle(m.get("title").toString());
        project.setIntroduction(m.get("introduction").toString());
        project.setTypename(m.get("typename").toString());
        String max = iProjectService.selectMaxProjectNum();
        if (max != null & !max.equals("null") & !max.equals("") & max.length() >= 7) {
            max = max.substring(2, max.length());
            max = "P-" + (Integer.parseInt(max) + 1);
            project.setNumber(max);
        }
        project.setReleasetime(DateUtil.getCurrentDate());
        System.out.println("InsertProject:" + project.toString());
        int result = iProjectService.insert(project);
        return result + "";
    }

    @RequestMapping(value = "/api/DeleteProjectById", produces = "text/plain;charset=utf-8")
    public String deleteProjectById(String id) {
        if (id == null) {
            return Constant.ERROR_10001 + "";
        }
        Long myid = Long.parseLong(id);
        int result = iProjectService.deleteByPrimaryKey(myid);
        System.out.println("DeleteResult:" + result);
        return result + "";
    }


   /* //并添加自己的图片  -->ajax
    @RequestMapping(value="ajaximage",produces="text/html;charset=utf-8",method= RequestMethod.POST)
    public Map<String, Object> ajaxImger(HttpServletRequest request,
                                         @RequestParam(value = "filename", required = false) MultipartFile filename, Integer id
    )throws IOException {
        System.out.println(id);
        Map<String,Object> resultMap = new HashMap<String, Object>();
        String filename2 = filename.getOriginalFilename();

        if (!filename2.isEmpty()) {
            // FileUtils 可以做文件流的拷贝 和 操作
            FileUtils.copyInputStreamToFile(
                    filename.getInputStream(),
                    new File("D:\\workspaces\\QQ\\WebContent\\images",
                            filename.getOriginalFilename()));
            //把图片路径保存到用户信息里面
            resultMap.put("img", "/images/"+filename.getOriginalFilename());
            resultMap.put("userid", id);
            userservice.UpadateImage(resultMap);
            resultMap.put("key", "success");
            return resultMap;
        }else{
            resultMap.put("key", "error");
            return resultMap;
        }
    }*/

}
